package com.posjiajia.user.operator.controller;

import com.posjiajia.commom.utils.vo.result.BaseResult;
import com.posjiajia.user.core.bo.UserBo;
import com.posjiajia.user.core.contant.Constants;
import com.posjiajia.user.core.params.UserQueryParam;
import com.posjiajia.user.core.po.UserPo;
import com.posjiajia.user.core.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author zhenhua.sun
 * @title com.posjiajia.user.operator.controller
 * @date 8/30/16
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserBo userBo;

    @RequestMapping("post")
    @ResponseBody
    public BaseResult postUser(UserVo userVo){
        BaseResult resultParam = checkUserParam(userVo);
        if (resultParam.getStatus() != BaseResult.SUCCESS.getStatus()){
            return resultParam;
        }
        if (userVo.getUserId() == null) {
            //新增user
            BaseResult resultUser = checkUserIsExist(userVo);
            if (resultUser.getStatus() != BaseResult.SUCCESS.getStatus()) {
                return resultUser;
            }
            //创建的操作员默认密码是123456
            userVo.setPassword(Constants.DEFAULT_PWD);
            userVo.setIsAdmin("".equals(userVo.getIsAdmin())?"0":userVo.getIsAdmin());
            userBo.userRegister(userVo);
        }else {
            //更新user
            userBo.updateUser(userVo);
        }

        return BaseResult.SUCCESS;
    }

    /**
     * 查询用户
     * @return
     */
    @RequestMapping("query")
    @ResponseBody
    public List<UserVo> queryUser(UserQueryParam userQueryParam){
        return userBo.query(userQueryParam);
    }

    /**
     * 删除用户
     * @return
     */
    @RequestMapping("delete")
    public String queryUser(String userId){
        userBo.delete(userId);
        return "user/userList";
    }



    /**
     * 校验用户注册参数
     * @param vo
     */
    private BaseResult checkUserParam(UserVo vo) {
        BaseResult result = new BaseResult();
        if (vo == null){
            result.setMessage("参数异常");
            return result;
        }
        if (StringUtils.isEmpty(vo.getEmail()) && StringUtils.isEmpty(vo.getMobilePhoneNo())){
            result.setMessage("用户邮箱或手机号不能都为空");
            return result;
        }

        return BaseResult.SUCCESS;
    }

    /**
     * 注册校验用户是否存在
     * @param vo
     * @return
     */
    private BaseResult checkUserIsExist(UserVo vo){
        BaseResult result = new BaseResult();
        if (!"".equals(vo.getEmail())) {
            UserPo userPo1 = userBo.getUserByPin(vo.getEmail());
            if (userPo1 != null) {
                result.setMessage("已存在该邮箱");
                return result;
            }
        }
        if (!"".equals(vo.getMobilePhoneNo())) {
            UserPo userPo2 = userBo.getUserByPin(vo.getMobilePhoneNo());
            if (userPo2 != null) {
                result.setMessage("已存在该手机号");
                return result;
            }
        }
        return BaseResult.SUCCESS;
    }
}
